import axios from '../../http/http.js'
const state = {
    tableList: [], //要渲染的表格数据
    maxPage: 1, //最大页数
    now: 1, //当前页
    max: 12, //总数据长度
    allList:[],
    pageLength:4
};
const actions = {
    getTable({ commit },data) {//获取列表数据
       commit('addtableData', data)
    },
    changePage({ commit }, currentPage) {//触发当前页改变事件
      console.log('当前页',currentPage)
        commit('changePage',currentPage)
    }

};
const mutations = {
    addtableData(state, data) {//列表数据进行存值
        state.allList=data;
        state.maxPage = Math.ceil(data.length / state.pageLength);
        state.max = data.length
    },
    changePage(state, index) {//改变当前页
        state.now=index;
    },
    setPageLenght(state,lang){//改变一页显示的长度
    console.log(lang)
    state.pageLength=lang;

  },
}
const getters = {
    mylist(state) {//监听数据的的当前页和总数据自动改变表格渲染数据
        return {
            tableList: state.allList.filter((item, index) => {
                return index < state.now * state.pageLength && index >= (state.now - 1) * state.pageLength
            }),
            maxPage: Math.ceil(state.allList.length / state.pageLength),
            now: state.now,
            max: state.allList.length,
            pageLength:state.pageLength
        }
    }
};
export default {
    state,
    actions,
    mutations,
    getters
}
